package com.xuzz.study.spring.bean.lifecyle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * 实现BeanPostProcessor接口可以在Bean(实例化之后)初始化的前后做一些自定义的操作，
 * 但是拿到的参数只有BeanDefinition实例和BeanDefinition的名称，也就是无法修改BeanDefinition元数据,这里说的Bean的初始化是： *
 * 1）bean实现了InitializingBean接口，对应的方法为afterPropertiesSet
 * 2）在bean定义的时候，通过init-method设置的方法
 *
 * PS:BeanFactoryPostProcessor回调会先于BeanPostProcessor
 */

public class ExampleBeanPostProcessor implements BeanPostProcessor {
    public ExampleBeanPostProcessor(){
        super();
        System.out.println("【ExampleBeanPostProcessor】的构造函数执行");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(bean  instanceof ExampleBean) {
            System.out.println(("【ExampleBeanPostProcessor】postProcessBeforeInitialization"+((ExampleBean)bean).info()));
        }
        System.out.println("【ExampleBeanPostProcessor】的postProcessBeforeInitialization初始化对象前执行");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(bean  instanceof ExampleBean) {
            System.out.println(("【ExampleBeanPostProcessor】postProcessAfterInitialization"+((ExampleBean)bean).info()));
        }
        System.out.println("【ExampleBeanPostProcessor】的postProcessAfterInitialization执行");
        return bean;
    }
}
